package org.mozhu.mboot.core.web.resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import javax.servlet.ServletContext;

@Component
public class BaseWebResourceConfigurer implements WebResourceConfigurer, ServletContextAware {

    protected ServletContext servletContext;

    @Override
    public void addResourceHandler(ResourceHandlerRegistry registry) {
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    protected String makeSureResourcePathEndswithSlash(String path) {
        if (StringUtils.isNotBlank(path) && !path.endsWith("/")) {
            path += "/";
        }
        return path;
    }

    protected String trimLastSlash(String path) {
        if (StringUtils.isNotBlank(path) && path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        return path;
    }
}
